package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.user.entity.ApAuthor;
import com.heima.user.dto.AuthDto;
import com.heima.user.dto.WmUser;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.feign.ArticleFeign;
import com.heima.user.feign.MediaFeign;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.service.IApUserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-03-11
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {
    @Autowired
    private IApUserService userService;

    @Autowired
    private MediaFeign mediaFeign;

    @Autowired
    private ArticleFeign articleFeign;
    @Override
    public ResponseResult listByStataus(AuthDto dto) {
        //构建条件
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!StringUtils.isEmpty(dto.getStatus()),ApUserRealname::getStatus,dto.getStatus());
        IPage<ApUserRealname> page = new Page<>(dto.getPage(),dto.getSize());
        page=page(page,queryWrapper);
        PageResponseResult pageResponseResult = new PageResponseResult(page.getCurrent(), dto.getSize(), page.getTotal(), page.getRecords());
        //初始化默认状态码为成功
        return pageResponseResult;
    }

    @Override
    @GlobalTransactional
    public ResponseResult auth(AuthDto dto, int type) {
        //首先根据id查询用户认证表
        ApUserRealname apUserRealname = getById(dto.getId());
        if(type==0)
        {
            //不通过，设置状态为2，并记录拒绝的原因
            apUserRealname.setStatus(2);
            apUserRealname.setReason(dto.getReason());
            updateById(apUserRealname);
            return ResponseResult.okResult();
        }
        //查询用户注册表
        ApUser apUser = userService.getById(apUserRealname.getUserId());
        //设置状态为通过
        apUserRealname.setStatus(9);
        //更新
        updateById(apUserRealname);
        //接下来创建自媒体账号
        WmUser wmuser=createWmUser(apUser);
        if(wmuser!=null)
        {   //创建文章作者
            ApAuthor apAuthor=createAuthor(apUser,wmuser);
            if(apAuthor!=null)
            {   //更新自媒体账号
                int i = 1 / 0;
                wmuser.setApAuthorId(apAuthor.getId());
                apUser.setFlag(1);
                userService.updateById(apUser);

            }
        } return ResponseResult.okResult();


    }


    /**
     * 创建自媒体用户
     *
     * @param apUser
     * @return
     */
    private WmUser createWmUser(ApUser apUser) {
        // 远程调用自媒体服务创建用户
        // 构建WmUser
        WmUser wmUser = new WmUser();
        BeanUtils.copyProperties(apUser, wmUser);
        wmUser.setId(null);
        wmUser.setApUserId(apUser.getId());
        wmUser.setNickname(apUser.getName());
        wmUser.setStatus(9);
        wmUser.setType(0);
        ResponseResult<WmUser> wmUserResponseResult = mediaFeign.saveWmUser(wmUser);
        if (wmUserResponseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {
            WmUser user = wmUserResponseResult.getData();
            return user;
        }
        return null;
    }


    /**
     * 创建文章作者
     *
     * @param apUser
     * @param wmUser
     * @return
     */
    private ApAuthor createAuthor(ApUser apUser, WmUser wmUser) {
        ApAuthor author = new ApAuthor();
        author.setName(apUser.getName());
        author.setUserId(apUser.getId());
        author.setWmUserId(wmUser.getId());
        author.setType(2);
        ResponseResult<ApAuthor> apAuthorResponseResult = articleFeign.saveApAuthor(author);
        if (apAuthorResponseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {
            ApAuthor apAuthor = apAuthorResponseResult.getData();
            return apAuthor;
        }
        return null;
    }

}
